package com.example.expandablelistview.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.expandablelistview.R;

public class ListAddDelSingleChoice extends Activity {
	private ArrayList<String> arItems;
	private ArrayAdapter<String> adapter;
	private ListView listView;
	private EditText mEdit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listadddelsinglechoice);
		
		arItems = new ArrayList<String>();
		arItems.add("First");arItems.add("Second");arItems.add("Third");
		
		adapter = new ArrayAdapter<String>(
				this, 
				android.R.layout.simple_list_item_single_choice, 
				arItems);
		
		listView = (ListView) findViewById(R.id.adddel_list);
		listView.setAdapter(adapter);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), adapter.getItem(position), Toast.LENGTH_LONG).show();
			}
		});
		mEdit = (EditText) findViewById(R.id.newItem);
	}
	
	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.btnAdd:
			String str = mEdit.getText().toString().trim();
			if (str.length() != 0){
				arItems.add(str);
				mEdit.setText("");
			}
			break;
		case R.id.btnDel:
			int selPos = listView.getCheckedItemPosition();
			if (selPos != ListView.INVALID_POSITION){
				arItems.remove(selPos);
				listView.clearChoices();
			}
			break;
		}
		adapter.notifyDataSetChanged();
	}
}
